<template>
    <!--组建教学班-->
    <div class="exam" v-if="tclassDetail">
        <!--过滤栏-->
        <div class="filter-container">
            <div class="filter-item item-input">
                <el-input v-model="tclassInput" placeholder="请输入教学班名称"></el-input>
            </div>
            <div class="filter-item item-button">
                <el-button type="primary" @click="searchTClassByName()"><i class="el-icon-search"></i> 搜索</el-button>
            </div>
            <div class="filter-item item-button">
                <el-button type="primary" @click="requestTclass(), tclassInput = ''"><i class="el-icon-refresh"></i> 刷新</el-button>
            </div>
            <div class="filter-item item-button">
                <el-button type="primary" @click="toCreateTClass = true,clearForm(),requestAllNclass()"><i class="el-icon-s-flag"></i> 组建教学班</el-button>
            </div>
        </div>

        <!--教学班列表-->
        <div class="exam-list">
            <el-table
                    :data="tclassDetail.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
                    border
                    style="width: 100%;">
                <el-table-column
                        label="序号"
                        width="60"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.$index + 1}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="教学班名称"
                        width="280"
                        align="center">
                    <template slot-scope="scope">
                        <el-tooltip content="点击查看教学班内详情" placement="top">
                            <span class="col2-cell-style"
                                  @click="viewNClassByTClass((currentPage - 1) * pageSize + scope.$index)">{{scope.row.name}}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="startTime"
                        label="创建时间"
                        width="200"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="endTime"
                        label="结束时间"
                        width="200"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="teacher"
                        label="任课老师"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="状态"
                        width="100"
                        align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" disable-transitions>正常</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="360">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                @click="changeTclass((currentPage - 1) * pageSize + scope.$index)">修改信息
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="deleteTclass((currentPage - 1) * pageSize + scope.$index)">删除班级
                        </el-button>
                        <!--<el-button-->
                                <!--size="mini"-->
                                <!--type="primary"-->
                                <!--@click="assignHomework((currentPage - 1) * pageSize + scope.$index)">布置作业-->
                        <!--</el-button>-->
                        <!--<el-button-->
                                <!--size="mini"-->
                                <!--type="primary"-->
                                <!--@click="downloadExcel((currentPage - 1) * pageSize + scope.$index)">下载成绩-->
                        <!--</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--分页-->
        <Pagination :currentPage="currentPage" :pageSize="pageSize" :total="tclassDetail.length"
                    :handleSizeChange="handleSizeChange" :handleCurrentChange="handleCurrentChange"></Pagination>

        <!--组建教学班 对话框-->
        <el-dialog title="组建教学班" width="90%" :visible.sync="toCreateTClass" top="2vh">
            <el-form ref="tclassForm" :model="tclassForm" status-icon :rules="tclassRules">
                <el-form-item label="教学班名称" :label-width="formLabelWidth" prop="name" >
                    <el-input v-model="tclassForm.name" autocomplete="off" placeholder="请输入班级名称" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="请选择自然班级"  prop="nclass" class="transfeTitle">
                </el-form-item>
                <div class="transfeLayout" >
                    <el-transfer
                            filterable
                            filter-placeholder="请输入自然班名称"
                            v-model="tclassForm.nclass"
                            :data="nclassData"
                            :titles="transferTitles"
                            :button-texts="transferButtonText">
                    </el-transfer>
                </div>

                <el-form-item label="结课时间" :label-width="formLabelWidth" prop="endTime">
                    <el-date-picker
                            v-model="tclassForm.endTime"
                            type="date"
                            format="yyyy 年 MM 月 dd 日"
                            value-format="timestamp"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="创建人" :label-width="formLabelWidth" prop="teacher" >
                    <el-input v-model="tclassForm.teacher" autocomplete="off" :disabled="true" style="width: 80%;"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="toCreateTClass = false, clearForm()">取 消</el-button>
                <el-button type="primary" @click="createTClass()">保 存</el-button>
            </div>
        </el-dialog>


        <!--查看该教学班有哪些自然班  对话框-->
        <el-dialog title="查看教学班内详情" :visible.sync="toViewNClassByTClass" width="80%" top="2vh">
            <el-table :data="nclassDetail">
                <el-table-column property="id" label="自然班级编号" width="220" align="center"></el-table-column>
                <el-table-column property="name" label="班级名称" width="300" align="center">
                    <template slot-scope="scope">
                        <el-tooltip content="点击查看该自然班学生" placement="top">
                            <span class="col2-cell-style"
                                  @click="viewStudentByNClass(scope.$index), eidInput = ''">{{scope.row.name}}</span>
                        </el-tooltip>

                    </template>
                </el-table-column>
                <el-table-column property="academyName" label="所属院系" width="300" align="center"></el-table-column>
                <el-table-column property="number" label="班级人数" width="100" align="center"></el-table-column>
                <el-table-column
                        prop="status"
                        label="状态"
                        align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" disable-transitions>正常</el-tag>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>


        <!--修改教学班内自然班  对话框-->
        <el-dialog title="修改教学班信息" width="90%" top="2vh" :visible.sync="toChangeTclass" >
            <el-form ref="tclassForm" :model="tclassForm" status-icon :rules="tclassRules">
                <el-form-item label="教学班名称" :label-width="formLabelWidth" prop="name" >
                    <el-input v-model="tclassForm.name" autocomplete="off" placeholder="请输入班级名称" style="width: 80%" :disabled="false"></el-input>
                </el-form-item>
                <el-form-item label="请选择自然班级"  prop="nclass" class="transfeTitle">
                </el-form-item>
                <div class="transfeLayout" >
                    <el-transfer
                            filterable
                            filter-placeholder="请输入自然班名称"
                            @change="handleChange"
                            v-model="tclassForm.nclass"
                            :data="nclassData"
                            :titles="transferTitles"
                            :button-texts="transferButtonText">
                    </el-transfer>
                </div>

                <el-form-item label="结课时间" :label-width="formLabelWidth" prop="endTime">
                    <el-date-picker
                            v-model="tclassForm.endTime"
                            type="date"
                            format="yyyy 年 MM 月 dd 日"
                            value-format="timestamp"
                            placeholder="选择日期"
                            :disabled="false">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="创建人" :label-width="formLabelWidth" prop="teacher" >
                    <el-input v-model="tclassForm.teacher" autocomplete="off" :disabled="true" style="width: 80%;"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="toChangeTclass = false, clearForm()">取 消</el-button>
                <el-button type="primary" @click="changeTClassComfirm(curClassIndex)">保 存</el-button>
            </div>
        </el-dialog>


        <el-dialog title="查看自然班学生详情" :visible.sync="toViewStudent" fullscreen>
            <div slot="title" class="header-title">
                <span>{{ tclassTitle }}</span>
            </div>

            <!--过滤栏-->
            <div class="filter-bar">
                <el-input
                        placeholder="请输入学号搜索"
                        v-model="eidInput"
                        clearable
                        class="filter-item">
                </el-input>
                <el-button type="primary" class="filter-item" @click="searchStudentsById()">
                    <i class="el-icon-search "></i> 搜索
                </el-button>
                <el-button type="primary" class="filter-item item-margin-left-16"
                           @click="viewStudentByNClass(curNClassIndex), eidInput = ''">
                    <i class="el-icon-refresh"></i> 刷新
                </el-button>
                <el-button type="primary" class="filter-item item-margin-left-16" @click="toViewStudent = false">
                    <i class="el-icon-circle-close"></i> 关闭页面
                </el-button>
            </div>

            <!--学生表格-->
            <el-table
                    :data="studentDetail.slice((stuCurPage - 1) * stuPageSize, stuCurPage * stuPageSize)"
                    border
                    style="width: 100%"
                    id="exportTable">
                <el-table-column
                        label="序号"
                        width="100"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.$index + 1}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="学号"
                        prop="id"
                        width="320"
                        align="center">
                </el-table-column>
                <el-table-column
                        label="姓名"
                        prop="name"
                        width="320"
                        align="center">
                </el-table-column>
                <el-table-column
                        label="入学年份"
                        prop="year"
                        width="200"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="状态"
                        align="center">
                    <template slot-scope="scope">
                        <el-tag disable-transitions>正常</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作..." width="400">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                @click="preChangeNClass(scope.row)">转班
                        </el-button>
                        <el-button
                                size="mini"
                                type="info"
                                @click="resetPassword((stuCurPage - 1) * stuPageSize + scope.$index)">重置密码
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--分页-->
            <Pagination :currentPage="stuCurPage" :pageSize="stuPageSize"
                        :total="studentDetail.length" :handleSizeChange="handleSizeChange2"
                        :handleCurrentChange="handleCurrentChange2"></Pagination>

        </el-dialog>

        <!--学生转班 对话框-->
        <el-dialog title="学生转班" :visible.sync="toChangeNClass" top="6vh" width="36%">
            <el-form ref="ncChangeForm" :model="ncChangeForm" status-icon :rules="ncChangeFormRules">
                <el-form-item label="学生学号" prop="stuId">
                    <el-input v-model="ncChangeForm.stuId" autocomplete="off" clearable
                              :disabled="true"
                              placeholder="请输入学生学号"></el-input>
                </el-form-item>
                <el-form-item label="学生姓名" prop="stuName">
                    <el-input v-model="ncChangeForm.stuName" autocomplete="off" clearable
                              :disabled="true"
                              placeholder="请输入学生姓名"></el-input>
                </el-form-item>
                <el-form-item label="请选择要转的班级" prop="stuClass">
                    <el-select v-model="ncChangeForm.stuClass" clearable filterable placeholder="选择要转的班级（可搜索）">
                        <el-option
                                v-for="item in optNClasses"
                                :key="item.classId"
                                :label="item.className"
                                :value="item.classId">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeNClass">取 消</el-button>
                <el-button type="primary" @click="changeStuNClass">确认转班</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import Pagination from './Pagination';
    import Axios from 'axios';
    import md5 from 'js-md5';
    import FileSaver from 'file-saver';
    import XLSX from 'xlsx';
    import qs from 'qs';

    export default {
        name: "",
        data: function () {
            return {

                nclassDetail: [],
                curNClassIndex: 0,//查看自然班的班级序号


                toChangeNClass: false, // 学生是否转班
                stuIdInput: '', // 学号搜索框的值
                stuDetail: [], // 学生详细信息
                ncChangeForm: { //学生转班表单
                    stuId: '',
                    stuName: '',
                    stuClass: ''
                },
                ncChangeFormRules: { //学生转班表单规则
                    stuId: [
                        {required: true, message: '学生学号不可为空', trigger: 'blur'}
                    ],
                    stuName: [
                        {required: true, message: '学生姓名不可为空', trigger: 'blur'}
                    ],
                    stuClass: [
                        {required: true, message: '请选择要转的班级', trigger: 'blur'}
                    ]
                },
                optNClasses: [], //学生转班可选的自然班级
                questCurPage: 1, // 题库当前页码
                questPageSize: 20, // 题库每页条目个数
                stuCurPage: 1,  //学生
                stuPageSize: 20,
                questKeyInput: '',
                tclassInput: '',
                toAddQuestion: false,
                toCreateHomework: false,
                toReviewStuPaper: false,
                toViewHomeworkByClass: false,
                toViewHomeworkDetail: false,
                toViewStudent: false,
                toViewNClassByTClass: false,
                toChangeHomeworkTime: false,
                toEndHomework: false,
                toDeleteTClass: false, //是否删除教学班对话框
                toCreateTClass: false, //是否创建教学班对话框
                toViewTclassDetail: false, //查看教学班对话框
                toChangeTclass: false, //增删教学班内自然班对话框
                tclassDetail: [],     //教学班列表
                allTClasses: [],     //所有教学班级
                currentPage: 1,
                pageSize: 20,
                curClassIndex: 0,//查看教学班的班级序号
                curStuIndex: 0,//查看学生的学生序号
                curHomeworkIndex: 0,//查看作业的作业序号
                curProblemIndex: 0,//创建作业的题目序号
                tclassForm: {
                    name: '',
                    nclass: [],
                    endTime: '',
                    teacher: localStorage.getItem("teacherName"), //在点击组建教学班时，自动将老师名字填入表格中，且无法修改。
                },
                transferTitles: ['未选择','已选择'], //组建班级对话框中穿梭框的标题
                transferButtonText: ['删除','添加'], //组建班级对话框中穿梭框的两个按钮
                // tclassData: [ {     //选中的教学班内有哪些自然班
                //     id: 201900003,
                //     name: '2019材料科学与工程1班',
                //     acadimicName: '材料科学与工程学院',
                // }, {
                //     id: 201900006,
                //     name: '2019材料科学与工程4班',
                //     acadimicName: '马克思主义学院',
                // }, {
                //     id: 201900007,
                //     name: '2019金属材料工程1班',
                //     acadimicName: '材料科学与工程学院',
                // }, {
                //     id: 201900008,
                //     name: '2019金属材料工程2班',
                //     acadimicName: '碧泉书院',
                // }],

                tclassHomeworkDetail: [ {     //选中的教学班内有哪些作业
                    id: 1000,
                    name: 'A + B problem',
                    type: '简单编程',
                    endTime: '2020-01-16 14:15:12',
                    endTimestamp: 1582732800000,
                    startTimestamp: 159234100000,
                    status: 1,
                }, {
                    id: 1008,
                    name: '标题1',
                    type: '一维数组',
                    endTime: '2020-01-16 14:15:12',
                    endTimestamp: 1582732800000,
                    startTimestamp: 159234100000,
                    status: 0,
                }, {
                    id: 1009,
                    name: '标题2',
                    type: 'do-while和for',
                    endTime: '2020-01-16 14:15:12',
                    endTimestamp: 1582732800000,
                    startTimestamp: 159234100000,
                    status: 0,
                }, {
                    id: 1019,
                    name: '标题3',
                    type: '二维数组',
                    endTime: '2020-01-16 14:15:12',
                    endTimestamp: 1582732800000,
                    startTimestamp: 159234100000,
                    status: 0,
                }],

                formLabelWidth: '120px',
                tclassRules: {
                    name: [
                        {required: true, message: '请输入教学班名称', trigger: 'blur'},
                        {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
                    ],
                    nclass: [
                        {required: true, message: '请选择班级', trigger: 'blur'}
                    ],
                    endTime: [
                        {required: true, message: '请选择结束教学日期', trigger: 'blur'}
                    ],
                },
                nclassData: [],
                tclassTitle: '',
                // nclassData: [ {
                //     key: 201900001,
                //     label: '2019材料物理1班',
                // }, {
                //     key: 201900002,
                //     label: '2019材料物理2班',
                // }, {
                //     key: 201900003,
                //     label: '2019材料科学与工程1班',
                // }, {
                //     key: 201900004,
                //     label: '2019材料科学与工程2班',
                // }, {
                //     key: 201900005,
                //     label: '2019材料科学与工程3班',
                // }, {
                //     key: 201900006,
                //     label: '2019材料科学与工程4班',
                // }, {
                //     key: 201900007,
                //     label: '2019金属材料工程1班',
                // }, {
                //     key: 201900008,
                //     label: '2019金属材料工程2班',
                // }, {
                //     key: 201900009,
                //     label: '2019新能源材料与器件1班',
                // }, {
                //     key: 201900018,
                //     label: '2019中国共产党历史1班',
                // }],
                categValue: '', //试题类型搜索框
                homeworkTitle: '',
                eidInput: '',
                categOptions: [{
                    value: '选项0',
                    label: '图论',
                }, {
                    value: '选项1',
                    label: '动态规划',
                }, {
                    value: '选项2',
                    label: '基础编程',
                }],
                homeworkForm: {
                    name: '',
                    tclass: '',
                    dateTime: [],
                    teacher: localStorage.getItem("teacherName"),
                },
                qLib: [], //题库
                Lib: [{    //显示的题库
                    id: 1,
                    name: 'A + B Problem',
                    type: '基础编程',
                    stem: '简单描述',
                    source: '本校题库',
                    difficulty: 'easy',
                    isAdded: false,
                    detail: {
                        description: '题目描述',
                        input: 'input',
                        output: 'output',
                        inputSample: 'inputSample',
                        outputSample: 'outputSample',
                    }
                }, {
                    id: 2,
                    name: 'A + B Problem 1',
                    type: '基础编程1',
                    stem: '简单描述1',
                    source: '本校题库',
                    difficulty: 'medium',
                    isAdded: false,
                    detail: {
                        description: '题目描述',
                        input: 'input',
                        output: 'output',
                        inputSample: 'inputSample',
                        outputSample: 'outputSample',
                    }
                }],
                homeworkRules: {
                    dateTime: [
                        {required: true, message: '请选择日期', trigger: 'blur'}
                    ],
                },
                studentDetail: [],
                nClassStudents: [],
                // studentDetail: [{
                //     id: 201921002100,
                //     name: '张三',
                //     grade: 0,
                //     submitCount: 1,
                //     submitTime: '2020-1-11',
                //     status: 1,
                //     answer: '111111111',
                // }, {
                //     id: 201921002101,
                //     name: '李四',
                //     grade: 0,
                //     submitCount: 1,
                //     submitTime: '2020-1-11',
                //     status: 2,
                //     answer: '111111111',
                // }, {
                //     id: 201921002102,
                //     name: '王五',
                //     submitCount: 1,
                //     grade: 0,
                //     submitTime: '2020-1-11',
                //     status: 3,
                //     answer: '111111111',
                // }, {
                //     id: 201921002103,
                //     name: '赵六',
                //     submitCount: 1,
                //     grade: 0,
                //     submitTime: '2020-1-11',
                //     status: 0,
                //     answer: '111111111',
                // }],

            }
        },
        components: {
            Pagination
        },
        mounted() {
            if(localStorage.getItem("teacherToken") != null  && localStorage.getItem("loginStatus") == 'teacher'){
                this.requestTclass();
                this.requestAllNclass();
                this.requestLib();
                this.requestProblemType();
            }else{
                this.$router.push( { path: "/login" })
            }
        },
        methods: {

            //点击去转班（转班预操作）
            preChangeNClass(student) {
                this.ncChangeForm.stuId = student.id;
                this.ncChangeForm.stuName = student.name;
                this.ncChangeForm.stuClass = '';
                this.requestOptNClasses(student.id);
                this.toChangeNClass = true;
            },


            //获取学生转班可选的自然班级
            requestOptNClasses(stuId) {
                Axios({
                    method: 'post',
                    url: '/teacher/getOptNClasses',
                    data: {
                        stu_id: stuId,
                        teacherToken: localStorage.getItem("teacherToken")
                    },
                    transformRequest: [
                        function (data) {
                            let ret = '';
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret;
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data === 3) {
                        this.$router.push({path: "/login"})
                        return;
                    }
                    // console.log(res.data);
                    this.optNClasses = res.data;
                }).catch((err) => {
                    this.$message.error('系统内部出错，请联系管理员');
                    console.log(err);
                });
            },

            //学生转班
            changeStuNClass() {
                this.$refs["ncChangeForm"].validate((valid) => {
                    if (valid) {
                        Axios({
                            method: 'post',
                            url: '/teacher/setStuNClass',
                            data: {
                                class_id: this.ncChangeForm.stuClass,
                                stu_id: this.ncChangeForm.stuId,
                                teacherToken: localStorage.getItem("teacherToken")
                            },
                            transformRequest: [
                                function (data) {
                                    let ret = '';
                                    for (let it in data) {
                                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                                    }
                                    ret = ret.substring(0, ret.lastIndexOf('&'));
                                    return ret;
                                }
                            ],
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            },
                        }).then((res) => {
                            if (res.data === 3) {
                                this.$router.push({path: "/login"})
                                return;
                            }
                            // console.log(res.data);
                            if (res.data == "success") {
                                this.getStuListById(this.ncChangeForm.stuId);
                                this.cancelChangeNClass();
                                this.$message.success('转班成功');
                            }
                        }).catch((err) => {
                            this.$message.error('系统内部出错，请联系管理员');
                            console.log(err);
                        });
                    } else
                        return false;
                });
            },

            //取消转班
            cancelChangeNClass() {
                //重置表单
                this.$refs["ncChangeForm"].resetFields();
                this.toChangeNClass = false;
            },

            //根据学号查询学生列表
            getStuListById(stuId) {
                if (this.stuIdInput.length > 0) {
                    Axios({
                        method: 'post',
                        url: '/teacher/getStuListById',
                        data: {
                            stu_id: stuId,
                            teacherToken: localStorage.getItem("teacherToken")
                        },
                        transformRequest: [
                            function (data) {
                                let ret = '';
                                for (let it in data) {
                                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                                }
                                ret = ret.substring(0, ret.lastIndexOf('&'));
                                return ret;
                            }
                        ],
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                    }).then((res) => {
                        if (res.data === 3) {
                            this.$router.push({path: "/login"})
                            return;
                        }
                        // console.log(res.data);
                        if (res.data.length !== 0)
                            this.stuDetail = res.data;
                        else
                            this.$message.warning('抱歉没找到任何数据');

                    }).catch((err) => {
                        this.$message.error('系统内部出错，请联系管理员');
                        console.log(err);
                    });
                } else
                    this.$message.warning("请先输入学生学号");
            },

            //根据名称搜索教学班级
            searchTClassByName() {
                if (this.tclassInput.length > 0) {
                    let tClassesByName = [];
                    this.currentPage = 1;
                    for (let i = 0; i < this.allTClasses.length; i++) {
                        if (this.allTClasses[i].name.indexOf(this.tclassInput) >= 0) {
                            tClassesByName.push(this.allTClasses[i]);
                        }
                    }
                    this.tclassDetail = tClassesByName;
                } else
                    this.$message.warning("请先输入教学班名称关键词！");
            },
            //根据学号搜索学生
            searchStudentsById() {
                if (this.eidInput.length > 0) {
                    let nStusById = [];
                    this.stuCurPage = 1;
                    for (let i = 0; i < this.nClassStudents.length; i++) {
                        if (this.nClassStudents[i].id.indexOf(this.eidInput) >= 0)
                            nStusById.push(this.nClassStudents[i]);
                    }
                    this.studentDetail = nStusById;
                } else
                    this.$message.warning("请先输入学号！");
            },
            handleSizeChange3(val) {
                this.questPageSize = val;
            },
            handleCurrentChange3(val) {
                this.questCurPage = val;
            },
            handleSizeChange2(val) {
                this.stuPageSize = val;
            },
            handleCurrentChange2(val) {
                this.stuCurPage = val;
            },
            handleSizeChange(val) {
                this.pageSize = val;
            },
            handleCurrentChange(val) {
                this.currentPage = val;
            },
            //询问是否删除教学班对话框
            deleteDialog(index){
                this.toDeleteTClass = true;
                this.curClassIndex = index;
            },
            //确认删除教学班
            deleteTClass(index){
                this.tclassDetail.splice(index,1);
                this.$message('删除成功');
                this.toDeleteTClass = false;
            },
            //创建教学班
            createTClass(){
                let pattern = /[ `!@#$%^&*()_+=\[\]{};':"\\|,.<>\/?~]/;
                if (pattern.test(this.tclassForm.name)) {
                    this.$message.warning('班级名称不能包含特殊字符')
                    return;
                }
                this.$refs['tclassForm'].validate((valid) => {
                    if(valid){
                        // console.log(this.tclassForm.nclass);
                        // console.log(this.tclassForm.endTime);
                        let startTime = new Date().getTime();
                        let endTime = new Date(this.tclassForm.endTime);

                        if(startTime >= endTime){
                            this.$message.error("结课时间只能是现在时间之后");
                            return ;
                        }

                        let newTClass = {};
                        newTClass.class_id = null;
                        newTClass.class_name = this.tclassForm.name;
                        newTClass.teacher_id = localStorage.getItem("teacherId");
                        newTClass.create_time = startTime;
                        newTClass.end_time = this.tclassForm.endTime;
                        newTClass.status = 0;

                        Axios({
                            method: 'post',
                            url: '/teacher/createTClass',
                            data:{
                                teacherToken:localStorage.getItem("teacherToken"),
                                nClassesId:this.tclassForm.nclass,
                                tClass:newTClass,
                            },
                            // transformRequest: [
                            //     function (data) {
                            //         let ret = ''
                            //         for (let it in data) {
                            //             ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            //         }
                            //         ret = ret.substring(0, ret.lastIndexOf('&'));
                            //         return ret
                            //     }
                            // ],
                            headers: {
                                'Content-Type': 'application/json'
                            },
                        }).then((res) => {
                            if (res.data == 3){
                                this.$router.push( { path: "/login" })
                                return;
                            }
                            if(res.data.state == 0){
                                this.$message.error(res.data.message);
                                return;
                            }
                            if(res.data.state == 1){
                                this.$message({
                                    message: '创建教学班级成功',
                                    type: 'success',
                                });
                                this.toCreateTClass = false;
                                this.clearForm();
                                this.requestTclass();
                                return ;
                            }
                        }).catch((err) => {
                            console.log(err);
                        })



                    }
                })
            },
            //修改教学班内的自然班
            changeTclass(index){
                var that = this;
                this.curClassIndex = index;
                this.clearForm();
                let nclass = [];
                this.requestAllNclass();
                Axios({
                    method: 'post',
                    url: '/teacher/findNClassByTClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        classId:this.tclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data){
                        for(let i = 0 ; i < res.data.length ; i++){
                            let tempClass = {};
                            nclass.push(res.data[i].classId);
                            tempClass.key = res.data[i].classId;
                            tempClass.label = res.data[i].className;
                            this.nclassData.push(tempClass)
                        }
                    }
                }).catch((err) => {
                    console.log(err);
                });
                // console.log(nclass);

                this.tclassForm.name= this.tclassDetail[index].name,
                this.tclassForm.nclass= nclass,
                this.tclassForm.endTime= this.tclassDetail[index].endTimestamp,
                this.tclassForm.teacher= localStorage.getItem("teacherName"),
                this.toChangeTclass = true;
                // that.$nextTick(function () {
                //     console.log(2222222);
                //     that.toChangeTclass = true;
                // })
            },
            //清除表格
            clearForm(){
                this.tclassForm = {
                    name: '',
                    nclass: [],
                    endTime: '',
                    teacher: localStorage.getItem("teacherName"),
                }
            },
            clearHomeworkForm(){
                this.homeworkForm = {
                    name: '',
                    tclass: '',
                    dateTime: [],
                    teacher: localStorage.getItem("teacherName"),
                }
            },

            confirmDeleteTClass(index) {
                Axios({
                    method: 'post',
                    url: '/teacher/deleteNClassByTClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {

                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 0){
                        this.$message.error(res.data.message);
                        return;
                    }
                    if(res.data.state == 1){
                        this.$message({
                            message: '刪除成功',
                            type: 'success',
                        });
                        this.requestTclass();
                    }

                }).catch((err) => {
                    console.log(err);
                });
            },


            deleteTclass(index) {
                let _this = this;
                Axios({
                    method: 'post',
                    url: '/teacher/getNClassCountByTClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {

                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 0){
                        this.$message.error(res.data.message);
                        return;
                    }
                    if(res.data.state == 1){
                        Confirm.show("教学班级存在关联的自然班级，仍要删除？", function () {
                            _this.confirmDeleteTClass(index);
                        });
                    }
                    else if(res.data.state == -1) {
                        _this.confirmDeleteTClass(index);
                    }

                }).catch((err) => {
                    console.log(err);
                });
            },

            //确认修改教学班内信息
            changeTClassComfirm(index){
                // this.tclassDetail[index].nclass = this.tclassForm.nclass;

                //编辑教学班的名称以及结束时间
                let pattern = /[ `!@#$%^&*()_+=\[\]{};':"\\|,.<>\/?~]/;
                if (pattern.test(this.tclassForm.name)) {
                    this.$message.warning('试卷名称不能包含特殊字符')
                    return;
                }
                Axios({
                    method: 'post',
                    url: '/teacher/editTClassByClassId',
                    data:{
                        class_id: this.tclassDetail[index].id,
                        class_name: this.tclassForm.name,
                        end_time: this.tclassForm.endTime
                    },

                    headers: {
                        'Content-Type': 'application/json'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 0){
                        this.$message.error(res.data.message);
                        return;
                    }
                    if(res.data.state == 1){
                        this.$message({
                            message: '保存成功',
                            type: 'success',
                        });
                        this.requestTclass();
                        this.toChangeTclass = false;
                    }

                }).catch((err) => {
                    console.log(err);
                });

            },
            //获取教学班
            requestTclass(){
                this.currentPage = 1;
                Axios({
                    method: 'post',
                    url: '/teacher/findTClassByTeacherId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        teacherId:localStorage.getItem("teacherId"),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data){
                        this.tclassDetail = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let tclass = {};
                            tclass.id = res.data[i].class_id;
                            tclass.name = res.data[i].class_name;
                            tclass.startTimestamp = res.data[i].create_time;
                            tclass.startTime = this.getTime(res.data[i].create_time);
                            tclass.endTimestamp = res.data[i].end_time;
                            tclass.endTime = this.getTime(res.data[i].end_time);
                            // ;tclass.nclass = [201900008,201900026]
                            tclass.teacher = localStorage.getItem("teacherName");
                            this.tclassDetail.push(tclass);
                        }
                        this.allTClasses = this.tclassDetail;
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //获取教学班的自然班(template)
            requestNclass(tclass){
                Axios({
                    method: 'post',
                    url: '/teacher/findNClassByTClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        classId:tclass,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data){
                        this.tclassData = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let nclass = {};
                            nclass.id = res.data[i].classId;
                            nclass.name = res.data[i].className;
                            nclass.acadimicName = res.data[i].academy_name;
                            this.tclassData.push(nclass);
                        }
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //获取未被其他教学班选取的自然班
            requestAllNclass(){
                Axios({
                    method: 'post',
                    url: '/teacher/getAllNClass',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data){
                        this.nclassData = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let nclass = {};
                            nclass.key = res.data[i].classId;
                            nclass.label = res.data[i].className;
                            this.nclassData.push(nclass);
                        }
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //修改自然班
            handleChange(value, direction, movedKeys) {
                // console.log(value, direction, movedKeys);
                //添加
                if(direction == 'right'){
                    Axios({
                        method: 'post',
                        url: '/teacher/addNclassFromTclass',
                        data:{
                            teacherToken:localStorage.getItem("teacherToken"),
                            tClassId:this.tclassDetail[this.curClassIndex].id,
                            nClassesId:movedKeys,
                        },
                        headers: {
                            'Content-Type': 'application/json'
                        },
                    }).then((res) => {
                        // console.log(res);
                        if (res.data == 3){
                            this.$router.push( { path: "/login" })
                            return;
                        }
                        if (res.data.state == 0){
                            this.$message.error("添加失败，请联系管理员");
                            return;
                        }
                        if (res.data.state == 1){
                            this.$message({
                                message: '添加成功',
                                type: 'success',
                            });
                            return;
                        }
                    }).catch((err) => {
                        console.log(err);
                    })
                }
                //删除
                else if(direction == 'left'){
                    Axios({
                        method: 'post',
                        url: '/teacher/delNclassFromTclass',
                        data:{
                            teacherToken:localStorage.getItem("teacherToken"),
                            tClassId:this.tclassDetail[this.curClassIndex].id,
                            nClassesId:movedKeys,
                        },
                        headers: {
                            'Content-Type': 'application/json'
                        },
                    }).then((res) => {
                        // console.log(res);
                        if (res.data == 3){
                            this.$router.push( { path: "/login" })
                            return;
                        }
                        if (res.data.state == 0){
                            this.$message.error("删除失败，请联系管理员");
                            return;
                        }
                        if (res.data.state == 1){
                            this.$message({
                                message: '删除成功',
                                type: 'success',
                            });
                            return;
                        }
                    }).catch((err) => {
                        console.log(err);
                    })
                }
            },
            requestLib(){
                Axios({
                    method: 'post',
                    url: '/teacher/getProblemLib',
                    data: {
                        teacherToken:localStorage.getItem("teacherToken"),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }
                    // console.log(res.data)
                    this.Lib = [];
                    this.qLib = [];
                    this.questCurPage = 1;
                    for (let i = 0; i < res.data.length; i++) {
                        let Lib = {};
                        Lib.id = res.data[i].problem_id;
                        Lib.name = res.data[i].problem_name;
                        Lib.type = res.data[i].problem_category_name;
                        Lib.stem = res.data[i].problem_description;
                        Lib.source = '本校题库';
                        Lib.difficulty = res.data[i].problem_tag_name;
                        Lib.isAdded = false;

                        let detail = {};
                        detail.description = res.data[i].problem_description;
                        detail.input = res.data[i].problem_input_format;
                        detail.output = res.data[i].problem_output_format;
                        detail.inputSample = res.data[i].problem_sample_input;
                        detail.outputSample = res.data[i].problem_sample_output;
                        //处理换行
                        detail.inputSample = detail.inputSample.replace(/\n/g, "<br>");
                        detail.outputSample = detail.outputSample.replace(/\n/g, "<br>");
                        detail.output = detail.output.replace(/\n/g, "<br>");
                        detail.input = detail.input.replace(/\n/g, "<br>");
                        detail.description = detail.description.replace(/\n/g, "<br>");
                        detail.outputSample = detail.outputSample.replace(new RegExp(/( )/g), "&nbsp;&nbsp;&nbsp;");
                        //处理图片
                        let temp = detail.description;
                        let picIndex = temp.indexOf('[1]:');
                        if (picIndex != -1) {
                            let picAddress = [];
                            picAddress = temp.substring(picIndex).replace('<br>', ',').replace(new RegExp(/( )/g), "").split(',');
                            for (let i = 0; i < picAddress.length; i++) {
                                picAddress[i] = "<img src='" + picAddress[i].substring(4) + "'>";
                            }
                            detail.description = detail.description.substring(0, picIndex);
                            for (let i = 0; i < picAddress.length; i++) {
                                let pic = detail.description.indexOf('![enter image description here]');
                                let head = detail.description.substring(0, pic);
                                let tail = detail.description.substring(pic + 34, detail.description.length);
                                detail.description = head + picAddress[i] + tail;
                            }
                        }
                        Lib.detail = detail;
                        this.Lib.push(Lib);
                        this.qLib.push(Lib);
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            assignHomework(index){
                this.toAddQuestion = true;
                this.curClassIndex = index;
                this.Lib = this.qLib;
                this.categValue = '';
            },
            addQuestion(index){
                this.homeworkForm.name = this.Lib[index].id + ' -- ' + this.Lib[index].name;
                this.homeworkForm.tclass = this.tclassDetail[this.curClassIndex].name;
                this.homeworkForm.dateTime = [];
                this.homeworkForm.teacher = localStorage.getItem("teacherName");
                this.curProblemIndex = index;
                this.toAddQuestion = false;
                this.toCreateHomework = true;
            },
            createHomework(){
                this.$refs['homeworkForm'].validate((valid) => {
                    if(valid || !valid){
                        let date = new Date();
                        let endTime = new Date(this.homeworkForm.dateTime[1]);
                        let startTime = new Date(this.homeworkForm.dateTime[0]);
                        if (endTime < date){
                            this.$message.error("结束时间必须大于当前时间");
                            return ;
                        }


                        Axios({
                            method: 'post',
                            url: '/teacher/assignHomework',
                            data: {
                                teacherToken:localStorage.getItem("teacherToken"),
                                class_id:this.tclassDetail[this.curClassIndex].id,
                                problem_id:this.Lib[this.curProblemIndex].id,
                                startTime:this.homeworkForm.dateTime[0].getTime(),
                                end_time:this.homeworkForm.dateTime[1].getTime(),
                                status:0,
                            },
                            headers: {
                                'Content-Type': 'application/json'
                            },
                        }).then((res) => {
                            if (res.data == 3){
                                this.$router.push( { path: "/login" })
                                return;
                            }

                            if(res.data.state == 0){
                                this.$message.error("布置作业失败！请联系管理员。");
                                return;
                            }
                            if(res.data.state == 1){
                                this.$message.error("该题目已被布置到这个教学班级");
                                return;
                            }
                            if(res.data.state == 2){
                                this.$message({
                                    message: '布置作业成功',
                                    type: 'success',
                                });
                                this.toCreateHomework = false;
                                this.clearHomeworkForm();
                            }
                        }).catch((err) => {
                            console.log(err);
                        })
                    }
                })
            },
            viewHomeworkByClass(index){
                this.curClassIndex = index;
                this.tclassHomeworkDetail = [];
                Axios({
                    method: 'post',
                    url: '/teacher/findHomeworkBytClassId',
                    data: {
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    // console.log(res.data)
                    if(res.data){
                        for(let i = 0 ; i < res.data.length ; i++){
                            let homeworkDetail = {};
                            homeworkDetail.id = res.data[i].problem_id;
                            homeworkDetail.name = res.data[i].problem_name;
                            homeworkDetail.description = res.data[i].problem_description;
                            homeworkDetail.type = res.data[i].problem_category_name;
                            homeworkDetail.status = res.data[i].status;
                            homeworkDetail.endTime = this.getTime(res.data[i].end_time);
                            homeworkDetail.endTimestamp = res.data[i].end_time;
                            homeworkDetail.startTime = this.getTime(res.data[i].start_time);
                            homeworkDetail.startTimestamp = res.data[i].start_time;
                            this.tclassHomeworkDetail.push(homeworkDetail);
                        }
                    }

                }).catch((err) => {
                    console.log(err);
                })
                this.toViewHomeworkByClass = true;
            },
            viewStudentByHomework(index){
                // console.log(this.tclassHomeworkDetail[index].id);
                this.homeworkTitle = this.tclassHomeworkDetail[index].name;
                this.curHomeworkIndex = index;
                Axios({
                    method: 'post',
                    url: '/teacher/findHomeworkDetailByIds',
                    data: {
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[this.curClassIndex].id,
                        problem_id:this.tclassHomeworkDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log('================')
                    // console.log(res.data);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data){
                        this.studentDetail = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let student = {};
                            student.id = res.data[i].stu_id;
                            student.name = res.data[i].stu_name;
                            student.submitCount = res.data[i].submit_count;
                            student.grade = res.data[i].score;
                            student.submitTime = res.data[i].submit_time == 0 ? null : this.getTime(res.data[i].submit_time);
                            student.status = res.data[i].status;
                            if (res.data[i].answer != null)
                                student.answer = res.data[i].answer.replace(/\n/g, "<br>").replace(/\s/g, "&nbsp;");
                            student.problemDescription = res.data[i].problem_description;
                            student.language = res.data[i].language;
                            this.studentDetail.push(student);
                        }
                    }


                }).catch((err) => {
                    console.log(err);
                })
                this.toViewHomeworkDetail = true;
            },
            //下载该教学班的所有作业成绩
            downloadExcel(index){
                console.log("downloadExcel");
                let downloadUrl = process.env.VUE_APP_URL+"/teacher/downloadScoreToExcelById?class_id="+this.tclassDetail[index].id+"&teacherToken="+localStorage.getItem("teacherToken");
                let a = document.createElement("a");
                a.href = downloadUrl;
                a.download = downloadUrl;
                a.click();
                return false;
            },
            //查阅学生答案
            reviewStuPaper(index){
                this.curStuIndex = index;
                this.toReviewStuPaper = true;
            },
            //修改学生成绩
            uploadScore(){
                Axios({
                    method: 'post',
                    url: '/teacher/modifyStudentScoreByIds',
                    data: {
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[this.curClassIndex].id,
                        problem_id:this.tclassHomeworkDetail[this.curHomeworkIndex].id,
                        stu_id:this.studentDetail[this.curStuIndex].id,
                        score:this.studentDetail[this.curStuIndex].grade,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 0){
                        this.$message.error("修改失败");
                        return;
                    }
                    if(res.data.state == 1){
                        this.$message({
                            message: '修改成功',
                            type: 'success',
                        })
                        this.toReviewStuPaper = false;
                        return ;
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //删除作业
            deleteHomework(index){
                Axios({
                    method: 'post',
                    url: '/teacher/delHomeworkByIds',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[this.curClassIndex].id,
                        problem_id:this.tclassHomeworkDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 1){
                        this.$message({
                            message: '删除成功',
                            type: 'success',
                        })
                        this.tclassHomeworkDetail.splice(index,1);
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //结束作业
            endHomework(index){
                Axios({
                    method: 'post',
                    url: '/teacher/endHomeworkByIds',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[this.curClassIndex].id,
                        problem_id:this.tclassHomeworkDetail[index].id,
                        status: 1,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 1){
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        })
                        this.tclassHomeworkDetail[index].status = 1;
                        this.toEndHomework = false;
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            //修改作业时间
            changeHomeworkTime(index){
                this.clearHomeworkForm();
                this.curHomeworkIndex = index;
                this.toChangeHomeworkTime = true;
                this.homeworkForm.teacher = localStorage.getItem("teacherName");
                this.homeworkForm.dateTime[1] = this.tclassHomeworkDetail[index].endTimestamp;
                this.homeworkForm.dateTime[0] = this.tclassHomeworkDetail[index].startTimestamp;
                this.homeworkForm.tclass = this.tclassDetail[this.curClassIndex].name;
                this.homeworkForm.name = this.tclassHomeworkDetail[index].name;
            },
            changeHomeworkTimeComfirm(){
                Axios({
                    method: 'post',
                    url: '/teacher/modifyHEndTimeByIds',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        class_id:this.tclassDetail[this.curClassIndex].id,
                        problem_id:this.tclassHomeworkDetail[this.curHomeworkIndex].id,
                        end_time:this.homeworkForm.dateTime[1].getTime(),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }

                    if(res.data.state == 1){
                        this.$message({
                            message: '修改成功',
                            type: 'success',
                        })
                        this.toChangeHomeworkTime = false;
                        return ;
                    }
                    if(res.data.state == 0){
                        this.$message.error("修改失败");
                        return;
                    }
                }).catch((err) => {
                    console.log(err);
                })
            },
            requestProblemType(){
                Axios({
                    method: 'post',
                    url: '/teacher/getProblemType',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }
                    if (res.data){
                        this.categOptions = [];
                        for(let i = res.data.length - 1 ; i >= 0 ; i--){
                            let map = {};
                            map.value = i;
                            map.label = res.data[i];
                            this.categOptions.push(map);
                        }
                    }

                }).catch((err) => {
                    console.log(err);
                })
            },
            problemTypeSearch(){
                this.Lib = [];
                for(let i = 0 ; i < this.qLib.length ; i++){
                    if(this.qLib[i].type == this.categValue){
                        this.Lib.push(this.qLib[i]);
                    }
                }
            },
            viewNClassByTClass(index){
                this.curClassIndex = index;
                Axios({
                    method: 'post',
                    url: '/teacher/findNClassByTClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        classId :this.tclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }
                    if(res.data){
                        this.nclassDetail = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let nclass = {};
                            nclass.id = res.data[i].classId;
                            nclass.name = res.data[i].className;
                            nclass.number = res.data[i].count;
                            nclass.academyName = res.data[i].academy_name;
                            nclass.status = res.data[i].status;
                            this.nclassDetail.push(nclass);
                        }
                        this.toViewNClassByTClass = true;
                    }

                }).catch((err) => {
                    console.log(err);
                })
            },
            viewStudentByNClass(index){
                this.curNClassIndex = index;
                this.tclassTitle = this.nclassDetail[index].name;
                this.stuCurPage = 1;
                Axios({
                    method: 'post',
                    url: '/teacher/findAllStuBynClassId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        nClassId:this.nclassDetail[index].id,
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }
                    if(res.data){
                        this.studentDetail = [];
                        for(let i = 0 ; i < res.data.length ; i++){
                            let student = {};
                            student.id = res.data[i].stu_id;
                            student.name = res.data[i].stu_name;
                            student.year = res.data[i].year;
                            this.studentDetail.push(student);
                        }
                        this.nClassStudents = this.studentDetail;
                        this.toViewStudent = true;
                    }

                }).catch((err) => {
                    console.log(err);
                })
            },
            resetPassword(index){
                Axios({
                    method: 'post',
                    url: '/teacher/resetStuPwdByStuId',
                    data:{
                        teacherToken:localStorage.getItem("teacherToken"),
                        stu_id:this.studentDetail[index].id,
                        password:md5("123456"),
                    },
                    transformRequest: [
                        function (data) {
                            let ret = ''
                            for (let it in data) {
                                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                            }
                            ret = ret.substring(0, ret.lastIndexOf('&'));
                            return ret
                        }
                    ],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((res) => {
                    // console.log(res);
                    if (res.data == 3){
                        this.$router.push( { path: "/login" })
                        return;
                    }
                    if(res.data.state == 0){
                        this.$message.error("重置失败，请联系管理员");
                        return ;
                    }
                    if(res.data.state == 1){
                        this.$message({
                            message : '重置成功',
                            type: 'success',
                        });
                        return ;
                    }

                }).catch((err) => {
                    console.log(err);
                })
            }
        },

    }
</script>

<style scoped>
    .exam {
        width: 100%;
    }

    .filter-container {
        padding-bottom: 10px;
    }

    .filter-item {
        display: inline-block;
        vertical-align: middle;
        margin-bottom: 10px;
    }

    .item-input {
        width: 200px;
        margin-right: 5px;
    }

    .filter-container .filter-item:nth-child(3), .filter-container .filter-item:nth-child(4) {
        margin-left: 16px;
    }

    .exam-list {
        width: 100%;
    }

    .pagination-container {
        margin-top: 30px;
    }

    .exam-list .col2-cell-style {
        color: #409EFF;
        cursor: pointer;
    }

    .filter-bar {
        width: 100%;
        padding-bottom: 10px;
    }

    .filter-bar .el-input {
        width: 200px;
        margin-right: 5px;
    }

    .filter-bar .item-margin-left-16 {
        margin-left: 16px;
    }

    .el-dialog .el-table .col2-cell-style {
        color: #409EFF;
        cursor: pointer;
    }

    .el-dialog .stu-paper-container {
        margin: 0 20px;
    }

    .el-dialog .stu-paper-container h1 {
        text-align: center;
        font-size: 2.5em;
    }

    .el-dialog .stu-paper-container h3 {
        text-align: left;
    }

    .el-dialog .stu-paper-container .el-row {
        margin-bottom: 30px;
    }

    .el-dialog .stu-paper-container .stu-answer {
        margin-left: 20px;
    }

    .el-dialog .stu-paper-container .stu-paper-footer {
        text-align: right;
        margin-top: 30px;
    }

    .el-dialog .paper-container {
        margin: 0 20px;
    }

    .el-dialog .paper-container .paper-header h1 {
        text-align: center;
        font-size: 2.5em;
    }

    .el-dialog .paper-container .paper-info {
        text-align: center;
        padding: 20px;
    }

    .el-dialog .paper-container .paper-info h3 {
        display: inline-block;
        width: 25%;
    }

    .paper-container .paper-content {
        padding: 20px;
    }

    .take-up-header {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .take-up-header .header-icon {
        display: flex;
        color: #f8bb86;
        position: relative;
        justify-content: center;
        width: 5em;
        height: 5em;
        margin: 0 auto 1.875em;
        border: .25em #facea8 solid;
        border-radius: 50%;
        line-height: 5em;
        box-sizing: content-box;
    }

    .take-up-header .header-icon .icon-text {
        font-size: 3.75em;
    }

    .take-up-header .header-title {
        display: block;
        position: relative;
        max-width: 100%;
        margin: 0 0 0.4em;
        padding: 0;
        color: #595959;
        font-size: 2em;
        font-weight: 600;
        text-align: center;
        text-transform: none;
        word-wrap: break-word;
    }

    .take-up-content {
        color: #545454;
        font-size: 1.25em;
        font-weight: 300;
        text-align: center;
    }

    .transfeLayout{
        margin-left: 120px;
        margin-bottom: 25px;

    }

    .transfeLayout >>> .el-transfer-panel {
        width:300px;
    }

    .transfeTitle{
        width: 120px;
        height: 0px;
        padding-left: 12px;
        margin-bottom: 0px;
    }

</style>